package WangYi;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int[] inorder = new int[n];
        int[] postorder = new int[n];

        for (int i = 0; i < n; i++) {
            inorder[i] = scanner.nextInt();
        }

        for (int i = 0; i < n; i++) {
            postorder[i] = scanner.nextInt();
        }

//        TreeNode treeNode = new Solution().buildTree(inorder, postorder);
//        int i = new Solution().maxLen(treeNode);
        TreeNode treeNode = new InorderPostOrderToTree().buildTree(inorder, postorder);
        int i = new FindMaxPath().maxLen(treeNode);
        System.out.println(i);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
        left = null;
        right = null;
    }
}

//把数组转换为二叉树
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return getNode(inorder, postorder, 0, inorder.length - 1, inorder.length - 1);
    }

    private TreeNode getNode(int[] inorder, int[] postorder, int instart, int inend, int postend) {
        if (instart > inend) {
            return null;
        }
        int nodeVal = postorder[postend];
        TreeNode node = new TreeNode(nodeVal);
        int nodeIndex = 0;

        for (int i = instart; i <= inend; i++) {
            if (nodeVal == inorder[i]) {
                nodeIndex = i;
                break;
            }
        }
        node.left = getNode(inorder, postorder, instart, nodeIndex - 1, postend - (inend - nodeIndex) - 1);
        node.right = getNode(inorder, postorder, nodeIndex + 1, inend, postend - 1);
        return node;
    }

    int ans = 1;
    public int maxLen(TreeNode root){
        depth(root);
        return ans - 1;
    }

    private int depth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftDept = depth(root.left);
        int rightDept = depth(root.right);

        ans = Math.max(ans, leftDept + rightDept+1);
        return Math.max(leftDept, rightDept) + 1;
    }
}

class InorderPostOrderToTree {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (inorder == null || inorder.length == 0) {
            return null;
        }
        return buildBinaryTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
    }

    private TreeNode buildBinaryTree(int[] inorder, int inleft, int inright, int[] postorder, int postLeft, int postRight) {
        if (inleft > inright) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[postRight]);
        if (inleft == inright) {
            return root;
        }
        int rootNum = 0;
        for (int i = inleft; i <= inright; i++) {
            if (inorder[i] == postorder[postRight]) {
                rootNum = i;
                break;
            }
        }
        int leftLength = rootNum - inleft;

        root.left = buildBinaryTree(inorder, inleft, inleft + leftLength - 1, postorder, postLeft, postLeft + leftLength - 1);
        root.right = buildBinaryTree(inorder, inleft + leftLength + 1, inright, postorder, postLeft + leftLength, postRight - 1);

        return root;
    }
}


class FindMaxPath{
    int ans = 1;
    public int maxLen(TreeNode root){
        depth(root);
        return ans - 1;
    }

    private int depth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftDept = depth(root.left);
        int rightDept = depth(root.right);

        ans = Math.max(ans, leftDept + rightDept+1);
        return Math.max(leftDept, rightDept) + 1;
    }
}